package com.nacrtstudio.webgiftshop.pages.admin;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.upload.services.UploadedFile;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.model.shop.Category;
import com.nacrtstudio.webgiftshop.model.shop.Gift;
import com.nacrtstudio.webgiftshop.model.shop.beans.GiftBean;
import com.nacrtstudio.webgiftshop.pages.StatusPage;

@RequiresRoles("admin")
public class AddNewGift {
	@Inject
	private Controller controller;
	@Property
	private Gift newGift;
	@Property
	private Category category;
	@Property
	private String categoryName;
	@Property
	private UploadedFile file;
	@Property
	private String giftName;
	@Property
	private String description;
	@Property
	private String altText;
	@Property
	private String price;
	@Property
	private boolean specialContent;
	@InjectComponent
	private Form form;
	@InjectComponent
	private Form addContent;
	@InjectComponent
	private Form addImage;

	
	public List<Category> getAllCategories() {
		return controller.getAllCategories();
	}
	
	public List<String> getAllCategoriesNames() {
		List<String> niz = new LinkedList<String>();
		for (int i = 0; i < getAllCategories().size(); i++) {
			niz.add(getAllCategories().get(i).getCategoryName());
		}
		return niz;
	}

	void onActivate(){
		if (imgList==null) {
			imgList = new LinkedList<String>();
		}
	}
	
	@Property
	private UploadedFile fileContent;
	@Persist
	private List<String> imgList;
	@Property
	private String imgPath;
	
	public List<String> getImgList() {
		return imgList;
	}

	public void setImgList(List<String> imgList) {
		this.imgList = imgList;
	}

	public void onSubmitFromAddContent() {
		try {
			File copied = new File("src/main/webapp/layout/images/" + fileContent.getFileName());
			fileContent.write(copied);
			imgList.add("layout/images/" + fileContent.getFileName());
		} catch (Exception e) {
			System.out.println("\n\n\n"+e+"\n\n\n");
		}
	}
	
	@Persist
	private String imagepath;
	
	public String getImagepath() {
		return imagepath;
	}

	public void setImagepath(String imagepath) {
		this.imagepath = imagepath;
	}

	public void onSubmitFromAddImage(){
		try {
			File copied = new File("src/main/webapp/layout/images/"
					+ file.getFileName());
			file.write(copied);
			setImagepath("layout/images/"+file.getFileName());
		} catch (Exception e) {
			System.out.println("\n\n\n\n\n"+e+"\n\n\n\n");		
		}
	}
	
	@InjectPage
	private StatusPage statusPage;
	public Object onSubmitFromForm() {
		try {
			controller.createGift(getImagepath(),giftName,description,altText,categoryName,price,imgList, specialContent);
			imgList = new LinkedList<String>();
			imagepath = "";
			return Catalog.class;
		} catch (Exception e) {
			statusPage.setMessage(e+"");
			return statusPage;
		}
	}
}
